package dbhelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import entities.User;


public class Users {
public static final String tableName = " ebaydb.users ";
private static final String registerUserSQL= 
"INSERT INTO" + tableName + "(user_name,display_name,salted_password,salt) VALUES (?,?,?,?);";
private static final String getUserSQL=
"SELECT * FROM" + tableName + "where user_name=?";
private static final String getUserByIDSQLQuery=
"SELECT * FROM" + tableName + "where id=?";
private static final String checkIfUserNameExists = "SELECT user_name FROM" + tableName + 
" WHERE user_name = ?";
/**
 * registers a new user on the db	
 * @return true if successful
 */
public static boolean register(User user){
	 PreparedStatement registerUserPS = null;
	 Connection connection = null;
	try {
		connection = MyDBConnectionPool.getConnection();
		registerUserPS = connection.prepareStatement(registerUserSQL);
		registerUserPS.setString(1, user.getUserName());
		registerUserPS.setString(2, user.getDisplayName());
		registerUserPS.setString(3, user.getSaltedPasswod());
		registerUserPS.setString(4, user.getSalt());
		return registerUserPS.executeUpdate() == 1;
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
		try {
			if(registerUserPS != null)
			registerUserPS.close();
			if(connection != null)
				connection.close();
		} catch (SQLException e) {
		e.printStackTrace();
		}
	}
	return false;
	
}

public static User getUser(User user){
	 PreparedStatement getUserPS = null;
	 ResultSet dbResult = null;
	 User result = null;
	 Connection connection = null;
		try {
			connection = MyDBConnectionPool.getConnection();
			getUserPS = connection.prepareStatement(getUserSQL);
			getUserPS.setString(1, user.getUserName());
			dbResult = getUserPS.executeQuery();
			if(dbResult.first()){
				result = new User();
				result.setID(dbResult.getLong("id"));
				result.setUserName(user.getUserName());
				result.setDisplayName(dbResult.getString("display_name"));
				result.setSaltedPasswod(dbResult.getString("salted_password"));
				result.setSalt(dbResult.getString("salt"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(getUserPS != null)
					getUserPS.close();
				if(dbResult != null)
					dbResult.close();
				if(connection != null)
					connection.close();
			} catch (SQLException e) {
			e.printStackTrace();
			}
		}
		return result;
}

public static User getUserByID(long userID){
	 PreparedStatement getUserPS = null;
	 ResultSet dbResult = null;
	 User result = null;
	 Connection connection = null;
		try {
			connection = MyDBConnectionPool.getConnection();
			getUserPS = connection.prepareStatement(getUserByIDSQLQuery);
			getUserPS.setLong(1, userID);
			dbResult = getUserPS.executeQuery();
			if(dbResult.first()){
				result = new User();
				result.setUserName(dbResult.getString("user_name"));
				result.setDisplayName(dbResult.getString("display_name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(getUserPS != null)
					getUserPS.close();
				if(dbResult != null)
					dbResult.close();
				if(connection != null)
					connection.close();
			} catch (SQLException e) {
			e.printStackTrace();
			}
		}
		return result;
}

public static boolean checkIfUserNameExists(User user){
	 PreparedStatement checkIfUserNameExistsPS = null;
	 Connection connection = null;
	try {
		connection = MyDBConnectionPool.getConnection();
		checkIfUserNameExistsPS = connection.prepareStatement(checkIfUserNameExists);
		checkIfUserNameExistsPS.setString(1, user.getUserName());
		return checkIfUserNameExistsPS.executeQuery().next();
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
		try {
			if(checkIfUserNameExistsPS != null)
				checkIfUserNameExistsPS.close();
			if(connection != null)
				connection.close();
		} catch (SQLException e) {
		e.printStackTrace();
		}
	}
	return false;
}
}
